// pages/profile/personality-type/index.ts
import tran from '../../../utils/language';
import { userInfoStepFive, groupSummaryList } from '../../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: tran._t('personalityType'),
    _tBase: tran._t('base'),
    joinGroup: false,
    navBarHeight: getApp().globalData.navBarHeight,
    joinGroupPopup: false,
    selected: '',
    pickerParams: {
      show: false,
      pickerType: 'MBTI',
      defaultValue: ''
    },
    formData: {
      personality: null,
      joinGroup: false
    },
    groupList: [
      {
        groupType: ''
      }
    ],
    memberAvatars: [],
    groupTitle: '',
    groupText: '',
    nextTapFlag: true,
    registerFlag: '',
    isEdit: getApp().globalData.profileFrom === 'edit'
  },
  querygroupSummaryList() {
    groupSummaryList({}).then((res: any) => {
      this.setData({
        groupList: res,
        groupText: res[3].groupIntro,
        groupTitle: res[3].groupTitle
      })
    })
  },
  /**
   * 点击复制
   */
  tapCopy() {
    wx.setClipboardData({
      data: "https://www.16personalities.com/ch",
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  overRegister() {
    const { formData } = this.data
    const params = formData
    this.setData({
      nextTapFlag: false
    })
    userInfoStepFive(params).then(res => {
      wx.setStorageSync('completedStepFlag', true)
      let loginInfo = wx.getStorageSync('loginInfo')
      // 重新调用用户信息的接口
      getApp().getUserInfo()
      if (loginInfo.completedStep <= 5) {
        loginInfo.completedStep = 5
        wx.setStorageSync('loginInfo', loginInfo)
      }
      wx.switchTab({
        url: "/pages/home/index"
      })
      setTimeout(() => {
        this.setData({
          nextTapFlag: true
        })
      }, 1000)
    })
  },
  toWebView() {
    // 
    wx.navigateTo({
      url: "/pages/profile/webView/webView?url=https://www.16personalities.com"
    })
  },
  isJoinGroup() {
    this.setData({ joinGroupPopup: true })
  },
  closePopup() {
    this.setData({ joinGroupPopup: false })
  },
  changeVisible() {
    const { formData } = this.data
    formData.joinGroup = !formData.joinGroup
    this.setData({ formData })
  },
  handlePicker(e: baseEvent) {
    // 打开picker 设置对应的picker弹出类型
    const type = e.target.dataset.type;
    const { personality } = this.data.formData
    this.setData({
      pickerParams: { show: true, pickerType: type || 'MBTI', defaultValue: personality }
    })
  },
  pickerConfirm(e: baseEvent) {
    const { formData } = this.data
    formData.personality = e.detail.value
    const type = e.detail.value
    this.setData({
      formData
    })
    const { groupList } = this.data
    const currentGroupList = groupList.length > 0 ? groupList.filter(item => item.groupType == type) : []
    const currentGroup: any = currentGroupList.length > 0 ? currentGroupList[0] : {}
    this.setData({
      groupTitle: currentGroup.groupTitle || 'INFP',
      groupText: currentGroup.groupIntro || 'INFP',
      memberAvatars: currentGroup.memberAvatars.slice(0, 3) || []
    })
    // 关闭弹框
    this.pickerCancel()
  },
  pickerCancel() {
    const { pickerParams } = this.data
    pickerParams.show = false
    this.setData({ pickerParams })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    const loginInfo = wx.getStorageSync('userInfo')
    const { formData, isEdit } = this.data
    if (isEdit) {
      formData.personality = loginInfo.personality
    }
    this.setData({
      loginInfo,
      registerFlag: options.type,
      formData
    })
    this.querygroupSummaryList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})